﻿namespace DeltaGroup.WheelOfJeopardy.UI.Screens
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    using DeltaGroup.WheelOfJeopardy.Model;
    using DeltaGroup.WheelOfJeopardy.UI.Events;
    using DeltaGroup.WheelOfJeopardy.UI.ViewModel;

    /// <summary>
    ///     Interaction logic for Board.xaml
    /// </summary>
    public partial class Board : UserControl
    {
        #region Fields

        /// <summary>
        /// 
        /// </summary>
        public static DependencyProperty DebugModeProperty = DependencyProperty.RegisterAttached("DebugMode", typeof(bool), typeof(Board));

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Board"/> class.
        /// </summary>
        public Board()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Board"/> class.
        /// </summary>
        /// <param name="debug">if set to <c>true</c> [debug].</param>
        public Board(bool debug)
        {
            DebugMode = debug;
        }

        #endregion Constructors

        #region Events

        /// <summary>
        /// Occurs when [category selected].
        /// </summary>
        public event EventHandler<CategorySelectedEventArgs> CategorySelected;

        #endregion Events

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether [debug mode].
        /// </summary>
        /// <value><c>true</c> if [debug mode]; otherwise, <c>false</c>.</value>
        public bool DebugMode
        {
            get; set;
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Shows the selectable categories.
        /// </summary>
        /// <param name="categories">The categories.</param>
        public void ShowSelectableCategories(IEnumerable<Category> categories)
        {
            var model = DataContext as NormalRoundViewModel;

            foreach (var category in categories)
            {
                var c = model.Categories.First(x => x.Category == category);
                c.AllowSelection = true;
            }
        }

        /// <summary>
        /// Handles the category selected.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void HandleCategorySelected(object sender, ExecutedRoutedEventArgs e)
        {
            var p = e.Parameter as Category;

            var model = DataContext as NormalRoundViewModel;

            foreach (var category in model.Categories)
            {
                category.AllowSelection = false;
            }

            if (CategorySelected != null)
                CategorySelected(this, new CategorySelectedEventArgs(p));
        }

        #endregion Methods
    }
}